/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Data;

import java.io.*;
import java.util.*;
import java.util.logging.*;

/**
 *
 * @author IZAYOI SAKUYA
 */
public class ADVAnalysis {
    private String dialogue[] = {"","","",""};

    private File scriptscanner;
    private Scanner sc;
    private boolean page = false;
    private int start;
    private HashMap<String,String> seldt;
    private String advfile;
    
    public ADVAnalysis(File script){
    	start = 0;
    	seldt = new HashMap<String,String>();
        try {
            scriptscanner = script;
            sc = new Scanner(scriptscanner);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ADVAnalysis.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Scan(){
        while(sc.hasNextLine() && dialogue[0].equals("")){
            advfile = sc.nextLine();
            Proccess(advfile);
        }
    }
    
    private void Scan(String s) {
        while(sc.hasNextLine()){
            String tags = sc.nextLine();
            if(tags.indexOf(":" + s) != -1){
                advfile = sc.nextLine();
                break;
            }
        }
        Scan();
    }
    
    private String Pretreatment(String advfile){
        if(advfile.indexOf("//") != -1 && advfile.indexOf("//") != 0){
            advfile = advfile.substring(0,advfile.indexOf("//")-1);
        }else if(advfile.indexOf("//") == 0){
            return " ";
        }
        advfile = advfile.replace("\t"," ");
        advfile = advfile.trim();
        advfile = advfile.replace("\\n","\n");

        while (advfile.contains("  "))
        {
        	advfile = advfile.replace("  "," ");
        }

        return advfile;
    }

    private void Proccess(String advfile){
    	if(advfile.length() == 0){
            return;
        }
        if(advfile.indexOf(":START") != -1){
            start=1;
            return;
        }
        if(start == 1){
        	advfile = Pretreatment(advfile);

            if(advfile.indexOf("\\p") != -1){
            	dialogue[0] = page?"1":"0";
                dialogue[1] = advfile.substring(0,advfile.length()-2);
                page = false;
                return;
            }
            if(advfile.indexOf("\\k") != -1){
            	dialogue[0] = page?"1":"0";
                dialogue[1] = advfile.substring(0,advfile.indexOf("\\k"))+advfile.substring(advfile.indexOf("\\k")+2);
                page = true;
                return;
            }
            if(advfile.indexOf("\\c") != -1){
            	dialogue[0] = page?"0":"1";
                String font1 = advfile.substring(2,3);
                String font2 = advfile.substring(4,5);
                
                dialogue[0] = "2";
                dialogue[1] = font1;
                dialogue[2] = font2;
                
                if (advfile.endsWith("\n"))
                {
                	dialogue[3] = advfile.substring(advfile.indexOf(";")+1);
                }
                
                page = true;
                return;
            }
            
            if(advfile.indexOf("Wait") != -1){
                String Wait[] = advfile.split(" ");
                dialogue[0] = "3";
                dialogue[1] = Wait[1];
                return;
            }
            
            if(advfile.indexOf("DispBG") != -1){
                String dispBG[] = advfile.split(" ");
                dialogue[0] = "4";
                dialogue[1] = dispBG[1].substring(1,dispBG[1].length()-1);
                return;
            }

            if(advfile.indexOf("ClrBG") != -1){
                String clearBG[] = advfile.split(" ");
                dialogue[0] = "5";
                dialogue[1] = clearBG[1];
                if (dialogue[1].equalsIgnoreCase("BG_BLACK"))
                {
                	dialogue[1] = "0";
                }
                if (dialogue[1].equalsIgnoreCase("BG_WHITE"))
                {
                	dialogue[1] = "1";
                }
                return;
            }
            
            if(advfile.indexOf("MoveBG") != -1){
                String moveBG[] = advfile.split(" ");
                dialogue[0] = "6";
                dialogue[1] = moveBG[1];
                return;
            }
            
            if(advfile.indexOf("DispCHA2") != -1){
                String dispCHA2[] = advfile.split(" ");
                String Parameters[] = dispCHA2[1].split(",");
                dialogue[0] = "7";
                dialogue[1] = Parameters[0].substring(1,Parameters[0].length()-1);;
                dialogue[2] = Parameters[1].substring(1,Parameters[1].length()-1);;
                dialogue[3] = Parameters[2];
                if (dialogue[3].equalsIgnoreCase("CHR_L"))
                {
                	dialogue[3] = "1";
                }
                if (dialogue[3].equalsIgnoreCase("CHR_R"))
                {
                	dialogue[3] = "2";
                }
                return;
            }
            
            if(advfile.indexOf("ClrCHA2") != -1){
                dialogue[0] = "8";
                return;
            }
            
            if(advfile.indexOf("DispCHA") != -1){
                String dispCHA[] = advfile.split(" ");
                String Parameters[] = dispCHA[1].split(",");
                dialogue[0] = "9";
                dialogue[1] = Parameters[0];
                dialogue[2] = Parameters[1].substring(1,Parameters[1].length()-1);
                dialogue[3] = Parameters[2];
                if (dialogue[3].equalsIgnoreCase("CHR_C"))
                {
                	dialogue[3] = "0";
                }
                if (dialogue[3].equalsIgnoreCase("CHR_L"))
                {
                	dialogue[3] = "1";
                }
                if (dialogue[3].equalsIgnoreCase("CHR_R"))
                {
                	dialogue[3] = "2";
                }
                return;
            }
            
            if(advfile.indexOf("ClrCHA") != -1){
                String clrCHA[] = advfile.split(" ");
                dialogue[0] = "10";
                dialogue[1] = clrCHA[1];
                return;
            }
            
            if(advfile.indexOf("MoveCHA") != -1){
                String moveCHA[] = advfile.split(" ");
                String Parameters[] = moveCHA[1].split(",");
                dialogue[0] = "11";
                dialogue[1] = Parameters[0];
                dialogue[2] = Parameters[1];
                if (dialogue[2].equalsIgnoreCase("CHR_L"))
                {
                	dialogue[2] = "1";
                }
                if (dialogue[2].equalsIgnoreCase("CHR_R"))
                {
                	dialogue[2] = "2";
                }
                return;
            }
            
            if(advfile.indexOf("TextMode") != -1){
                String textMode[] = advfile.split(" ");
                dialogue[0] = "12";
                dialogue[1] = textMode[1];
                return;
            }
            
            if(advfile.indexOf("Calender") != -1){
                String calender[] = advfile.split(" ");
                String Parameters[] = calender[1].split(",");
                dialogue[0] = "13";
                dialogue[1] = Parameters[0];
                dialogue[2] = Parameters[1];
                dialogue[3] = Parameters[2];
                return;
            }
            
            if(advfile.indexOf("TimeDisp") != -1){
                String timeDisp[] = advfile.split(" ");
                String Parameters[] = timeDisp[1].split(",");
                dialogue[0] = "14";
                dialogue[1] = Parameters[0];
                dialogue[2] = Parameters[1];
                return;
            }
            
            if(advfile.indexOf("PlayBGM") != -1){
                String playBGM[] = advfile.split(" ");
                dialogue[0] = "15";
                dialogue[1] = playBGM[1];
                return;
            }
            
            if(advfile.indexOf("StopBGM") != -1){
                dialogue[0] = "16";
                return;
            }
            
            if(advfile.indexOf("PlaySE") != -1){
                String SEtype[] = advfile.split(" ");
                dialogue[0] = "17";
                dialogue[1] = SEtype[1];
                return;
            }
            
            if(advfile.indexOf("StopSE") != -1){
                dialogue[0] = "18";
                return;
            }
            
            if(advfile.indexOf("Effect") != -1){
                String Effecttype[] = advfile.split(" ");
                dialogue[0] = "19";
                dialogue[1] = Effecttype[1];
                return;
            }
            
            if(advfile.indexOf("EffectOff") != -1){
                String EffectOfftype[] = advfile.split(" ");
                dialogue[0] = "20";
                dialogue[1] = EffectOfftype[1];
                return;
            }
            
            if(advfile.indexOf("Select seldt") != -1){
                dialogue[0] = "100";
                boolean isSelect = false;
                int selecttime = 0;
                while(true){
                    advfile = sc.nextLine();
                    advfile = Pretreatment(advfile);
                    if(advfile.indexOf("}") != -1){
                    	dialogue[1] = dialogue[1].substring(0,dialogue[1].length() - 1);
                        isSelect = false;
                        return;
                    }
                    if(isSelect && advfile.length() > 0){
                        selecttime++;
                        dialogue[1] += advfile + "\n";
                    }
                    if(advfile.indexOf("{") != -1){
                        isSelect = true;
                    }
                }
            }

            if(advfile.indexOf("goto") != -1){
                String selecttag[] = advfile.split(" ");
                sc.close();
                try {
                    sc = new Scanner(scriptscanner);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ADVAnalysis.class.getName()).log(Level.SEVERE, null, ex);
                }
                Scan(selecttag[1]);
            }

            if(advfile.indexOf("load") != -1){
                String filename[] = advfile.split(" ");
                sc.close();
                try {
                	scriptscanner = new File(scriptscanner.getParent() + "\\" + filename[1].substring(1,filename[1].length()-1));
                    sc = new Scanner(scriptscanner);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ADVAnalysis.class.getName()).log(Level.SEVERE, null, ex);
                }
                return;
            }
            
            if(advfile.contains("lf_")){
                if(advfile.indexOf("if") != -1){
                    String selecttag[] = advfile.split(" |=|,");
                    String seldtvalue = seldt.get(selecttag[1]);
                    if(seldtvalue.equalsIgnoreCase(selecttag[5])){
                        sc.close();
                        try {
                            sc = new Scanner(scriptscanner);
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(ADVAnalysis.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        Scan(selecttag[6]);
                    }
                }else{
                    String selecttag[] = advfile.split(" |=");
                    seldt.put(selecttag[0], selecttag[3]);
                }
                return;
            }

            if(advfile.startsWith("end")){
                dialogue[0] = "666";
                return;
            }
        }
        else
        {
        	if(advfile.indexOf("lf_") != -1){
                advfile = Pretreatment(advfile);
                String seldtvalue[] = advfile.split(" |=");
                seldt.put(seldtvalue[0], seldtvalue[3]);
                return;
            }
        }
    }

    /**
     * @return the dialogue
     */
    public String[] getDialogue(int seldt) {
        for (int i = 0;i < 4;i++)
        {
            dialogue[i] = "";
        }
        if(seldt == -1){
            Scan();
        }
        else{
        	advfile = "";
            while (!advfile.contains(seldt + ","))
            {
                advfile = sc.nextLine();
            }
            advfile = advfile.substring(advfile.indexOf(",")+1);
            sc.close();
            try {
                sc = new Scanner(scriptscanner);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ADVAnalysis.class.getName()).log(Level.SEVERE, null, ex);
            }
            Scan(advfile);
        }
        return dialogue;
    }
}
